' Search up through the tree, noting the node keys so that we can then locate the NetResource object
' under NetRoot.
Dim pS As String, kPath() As String, nX As NetResource, i As Integer, tX As NetResource
Set tNod = Node ' Start at the node that was expanded
Do While Not tNod.Parent Is Nothing ' Proceed up the tree using parent references, each time saving the node key to the string pS
pS = tNod.Key + "|" + pS
Set tNod = tNod.Parent
Loop
' String pS is now of the form "<Node Key>|<Node Key>|<Node Key>"
' Split this into an array using the VB6 Split function
kPath = Split(pS, "|")
Set nX = NetRoot
' Now loop through this array, this time following down the tree of NetResource objects from NetRoot to the child NetResource object that corresponds to
' the node the user clicked
For i = 0 To UBound(kPath) - 1
Set nX = nX.Children(kPath(i))
Next
' Now that we know both the node and the corresponding NetResource we can enumerate the children and add the nodes
For Each tX In nX.Children
Set tNod = tvwNetwork.Nodes.Add(nX.RemoteName, tvwChild, tX.RemoteName, tX.ShortName, LCase(tX.ResourceTypeName), LCase(tX.ResourceTypeName))
tNod.Tag = "N"
' Add fake nodes to all new nodes except when they're printers (you can always be sure a printer never has children)
If tX.ResourceType <> Printer Then tvwNetwork.Nodes.Add tX.RemoteName, tvwChild, tX.RemoteName + "_FAKE", "FAKE", "server", "server"
Next
tvwNetwork.Refresh ' Refresh the view
Node.Tag = "Y" ' Set the tag to "Y" to denote that this node has been expanded and populated
End If
End Sub
Private Sub Form_Load()
' Centre the form on the screen
Me.Top = (Screen.Height - Me.Height) / 2
Me.Left = (Screen.Width - Me.Width) / 2
Dim nX As NetResource, nodX As Node
tvwNetwork.ImageList = imlNWImages
Set NetRoot = New NetResource ' Create a new NetResource object. By default it will be the network root
Set nodX = tvwNetwork.Nodes.Add(, , "_ROOT", "Entire Network", "root", "root") ' Add a node into the tree for it
nodX.Tag = "Y" ' Set populated flag to "Y" since we populate this one immediately
' Populate the top level of objects under "Entire Network"
For Each nX In NetRoot.Children
Set nodX = tvwNetwork.Nodes.Add("_ROOT", tvwChild, nX.RemoteName, nX.ShortName, LCase(nX.ResourceTypeName), LCase(nX.ResourceTypeName))
nodX.Tag = "N" ' We haven't populated the nodes underneath this one yet, so set its flag to "N"
tvwNetwork.Nodes.Add nodX.Key, tvwChild, nodX.Key + "_FAKE", "FAKE", "server", "server" ' Create a fake node under it so that the treeview gives the "+" symbol
nodX.EnsureVisible
' You can't get printers at this level, so there's no point in enumerating the NWPrinters collections yet
End Sub
Private Sub Form_Resize()
tvwNetwork.Width = Me.ScaleWidth
tvwNetwork.Height = Me.ScaleHeight
End Sub
Private Sub tvwNetwork_Expand(ByVal Node As MSComctlLib.Node)